/************************************************************
 ********************** 左表格*******************************
 ***********************************************************/
/**
 /**
 * 表头值域列表初始化
 */
var SdsField = {
    id: "fieldTree",	//表格id
    seItem: null,		                //选中的条目
    table: null,
    ztree: null,
    layerIndex: -1,
    fieldId: 0,
    entityId: 0,
    title:null
};

/**
 * 根据病种id查找病种项目
 */
SdsField.onClickDicSub = function (e, treeId, treeNode) {
    if (treeNode.pId == null || treeNode.pId == 0) {
        Sys.info("不能选择根节点（"+treeNode.name+"），请重新选择具体表头。");
        return false;
    }
    console.log(treeNode);
    SdsField.fieldId = treeNode.id;
    SdsField.title = treeNode.name;
    SdsField.entityId = treeNode.pId;
    //重置中间区域
    SdsFormItemMatch.seItem = null;
    SdsFormItemMatch.formItemId = 0;
    $(".hisui-checkbox").checkbox("uncheck")
    //查询病种项目
    SdsFormItemMatch.search();
    //查询病种值域
    SdsFormDicMatch.search();
}

/************************************************************
 ********************** 右上表格部分*******************************
 ***********************************************************/
/**
 */
/**
 * 病种项目初始化
 */
var SdsFormItemMatch = {
    id: "SdsFormItemMatchTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    formItemId: 0,
    formItemDesc:null
};

/**
 * 初始化表格的列
 */
SdsFormItemMatch.initColumn = function () {
    return [
        {field: 'selectItem', radio: true, formatter : selectcheckbox},
        {title: '病种字典', field: 'entityId', align: 'center', valign: 'middle', sortable: true, visible:false},
        {title: '代码', field: 'btCode', width: 150,  align: 'left', valign: 'middle', sortable: true},
        {title: '描述', field: 'btDesc', width: 300, align: 'left', valign: 'middle', sortable: true},
        {title: '项目类型', field: 'btTypeDict.description', width: 100, align: 'center', valign: 'middle', sortable: true},
        {title: '项目大类', field: 'btItemCat', width: 100, align: 'center', valign: 'middle', sortable: true},
        {title: '项目子类', field: 'btItemSubCat', width: 100, align: 'center', valign: 'middle', sortable: true}
    ];
    function selectcheckbox(value, row, index) {
        if(row.isCheck){
            return {
                disabled : false,
                checked : true
            };
            return value;
        }
    }
};

/**
 * 对照
 */
SdsFormItemMatch.compare = function (){
    var selected = $('#SdsFormItemMatchTable').bootstrapTable('getSelections');
    if(SdsField.fieldId&&SdsField.fieldId!=0){
        /*var idStr = "";
        for (var i=0;i<selected.length;i++) {
            var tmprow = selected[i];
            var id = tmprow.id;
            idStr += id+",";
        }*/
        var ajax = new $ax(Sys.backendPath + "/sds/sdsindfieldconfig/compare", function (data) {
            Sys.success("对照成功!");
            SdsFormItemMatch.table.refresh();
            SdsFormDicMatch.table.refresh();
        }, function (data) {
            Sys.error("对照失败!" + data.responseJSON.message + "!");
        });
        ajax.set("fieldId",SdsField.fieldId);
        ajax.set("formDicId","");
        ajax.set("formItemId",selected[0].id);
        ajax.start();
    }else if (selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}

/**
 * 撤销
 */
/*$("#SdsFormItemMatchTable").on("uncheck.bs.table", function (e, row, $element) {
    //row - 当前的行对象，取值方式 row.属性
    var ajax = new $ax(Sys.backendPath + "/sds/sdsindfieldconfig/undo", function (data) {
        Sys.success("撤销成功!");
        SdsFormItemMatch.table.refresh();
        SdsFormDicMatch.table.refresh();
    }, function (data) {
        Sys.error("撤销失败!" + data.responseJSON.message + "!");
    });
    ajax.set("formItemId",row.id);
    ajax.set("formDicId","");
    ajax.set("fieldId",SdsField.fieldId);
    ajax.start();
});*/
SdsFormItemMatch.undo = function (){
    var selected = $('#SdsFormItemMatchTable').bootstrapTable('getSelections');
    if(SdsField.fieldId&&SdsField.fieldId!=0){
        /*var idStr = "";
        for (var i=0;i<selected.length;i++) {
            var tmprow = selected[i];
            var id = tmprow.id;
            idStr += id+",";
        }*/
        var ajax = new $ax(Sys.backendPath + "/sds/sdsindfieldconfig/undo", function (data) {
            Sys.success("撤销成功!");
            SdsFormItemMatch.table.refresh();
            SdsFormDicMatch.table.refresh();
        }, function (data) {
            Sys.error("撤销失败!!" + data.responseJSON.message + "!");
        });
        ajax.set("fieldId",SdsField.fieldId);
        ajax.set("formDicId","");
        ajax.set("formItemId",selected[0].id);
        ajax.start();
    }else if (selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}

/**
 * 未对照
 */
SdsFormItemMatch.compareNo = function (){
    if(SdsField.fieldId&&SdsField.fieldId!=0){
        var queryData = {};
        queryData['fieldId'] = SdsField.fieldId;
        queryData['entityId'] = SdsField.entityId;
        queryData['compare'] = "no";
        SdsFormItemMatch.table.refresh({query: queryData});
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }

}

/**
 * 已对照
 */
SdsFormItemMatch.compareYes = function (){
    if(SdsField.fieldId&&SdsField.fieldId!=0){
        var queryData = {};
        queryData['fieldId'] = SdsField.fieldId;
        queryData['entityId'] = SdsField.entityId;
        queryData['compare'] = "yes";
        SdsFormItemMatch.table.refresh({query: queryData});
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}

/**
 * 全部
 */
SdsFormItemMatch.compareAll = function (){
    if(SdsField.fieldId&&SdsField.fieldId!=0){
        var queryData = {};
        queryData['fieldId'] = SdsField.fieldId;
        queryData['entityId'] = SdsField.entityId;
        queryData['compare'] = "";
        SdsFormItemMatch.table.refresh({query: queryData});
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}

/**
 * 点击病种项目，加载对应病种值域
 */
SdsFormItemMatch.onClickDic = function (obj,$ele,field){
    debugger;
    SdsFormItemMatch.seItem = obj;
    SdsFormItemMatch.formItemId = obj.id;
    SdsFormItemMatch.primaryHeaderDesc = obj.title;
    SdsFormDicMatch.search();
}



/**
 * 查询病种项目关联
 */
SdsFormItemMatch.search = function () {
    var queryData = {};
    queryData['fieldId'] = SdsField.fieldId;
    queryData['entityId'] = SdsField.entityId;
    queryData['compare'] = "";
    queryData['btDesc'] = $("#seachItemKey").val();
    SdsFormItemMatch.table.refresh({query: queryData});
};

/**
 * 查询病种项目关键字
 */
$('#seachItemKey').keydown(function(event){
    if(event.keyCode==13){
        //grid.submitFilter();
        SdsFormItemMatch.search();
    }
});


/************************************************************
 ********************** 右下表格部分*******************************
 ***********************************************************/
/**
 /**
 * 病种值域初始化
 */
var SdsFormDicMatch = {
    id: "SdsFormDicMatchTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SdsFormDicMatch.initColumn = function () {
    return [
        {field: 'selectItem', checkbox: true,formatter : selectcheckbox},
        {title: '字典代码', field: 'code', align: 'center', valign: 'middle', sortable: true},
        {title: '字典描述', field: 'desc', align: 'center', valign: 'middle', sortable: true},
    ];
    function selectcheckbox(value, row, index) {
        if(row.isCheck){
            return {
                disabled : false,
                checked : true
            };
            return value;
        }
    }
};

/**
 * 检查是否选中
 */
SdsFormDicMatch.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdsFormDicMatch.seItem = selected[0];
        return true;
    }
};

/**
 * 对照
 */
/*SdsFormDicMatch.compare = function (){
    var selected = $('#SdsFormDicMatchTable').bootstrapTable('getSelections');
    if(SdsField.fieldId&&SdsField.fieldId!=0){
        var idStr = "";
        for (var i=0;i<selected.length;i++) {
            var tmprow = selected[i];
            var id = tmprow.id;
            idStr += id+",";
        }
        var ajax = new $ax(Sys.backendPath + "/sds/sdsindfieldconfig/compare", function (data) {
            Sys.success("对照成功!");
            SdsFormDicMatch.table.refresh();
        }, function (data) {
            Sys.error("对照失败!" + data.responseJSON.message + "!");
        });
        ajax.set("fieldId",SdsField.fieldId);
        ajax.set("formDicIds",idStr);
        ajax.set("formItemIds","");
        ajax.start();
    }else if (selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}*/
$("#SdsFormDicMatchTable").on("check.bs.table", function (e, row, $element) {
    //row - 当前的行对象，取值方式 row.属性
    if(SdsField.fieldId&&SdsField.fieldId!=0){
        var ajax = new $ax(Sys.backendPath + "/sds/sdsindfieldconfig/compare", function (data) {
            Sys.success("对照成功!");
            SdsFormDicMatch.table.refresh();
        }, function (data) {
            Sys.error("对照失败!" + data.responseJSON.message + "!");
        });
        ajax.set("fieldId",SdsField.fieldId);
        ajax.set("formDicId",row.id);
        ajax.set("formItemId","");
        ajax.start();
    }else {
        $element[0].checked = false;
        Sys.info("请先选中某个字典！");
        return false;
    }

});

/**
 * 撤销
 */
$("#SdsFormDicMatchTable").on("uncheck.bs.table", function (e, row, $element) {
    //row - 当前的行对象，取值方式 row.属性
    var ajax = new $ax(Sys.backendPath + "/sds/sdsindfieldconfig/undo", function (data) {
        Sys.success("撤销成功!");
        SdsFormDicMatch.table.refresh();
    }, function (data) {
        Sys.error("撤销失败!" + data.responseJSON.message + "!");
    });
    ajax.set("formItemId","");
    ajax.set("formDicId",row.id);
    ajax.set("fieldId",SdsField.fieldId);
    ajax.start();
});

/**
 * 未对照
 */
SdsFormDicMatch.compareNo = function (){
    if(SdsField.fieldId&&SdsField.fieldId!=0){
        var queryData = {};
        queryData['fieldId'] = SdsField.fieldId;
        queryData['formItemId'] = SdsFormItemMatch.formItemId;
        queryData['compare'] = "no";
        SdsFormDicMatch.table.refresh({query: queryData});
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }

}

/**
 * 已对照
 */
SdsFormDicMatch.compareYes = function (){
    if(SdsField.fieldId&&SdsField.fieldId!=0){
        var queryData = {};
        queryData['fieldId'] = SdsField.fieldId;
        queryData['formItemId'] = SdsFormItemMatch.formItemId;
        queryData['compare'] = "yes";
        SdsFormDicMatch.table.refresh({query: queryData});
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}

/**
 * 全部
 */
SdsFormDicMatch.compareAll = function (){
    if(SdsField.fieldId&&SdsField.fieldId!=0){
        var queryData = {};
        queryData['fieldId'] = SdsField.fieldId;
        queryData['formItemId'] = SdsFormItemMatch.formItemId;
        queryData['compare'] = "";
        SdsFormDicMatch.table.refresh({query: queryData});
    }else {
        Sys.info("请先选中某个字典！");
        return false;
    }
}
/**
 * 查询值域关联列表
 */
SdsFormDicMatch.search = function () {
    var queryData = {};
    queryData['fieldId'] = SdsField.fieldId;
    queryData['formItemId'] = SdsFormItemMatch.formItemId;
    queryData['desc'] = $("#seachDicKey").val();
    SdsFormDicMatch.table.refresh({query: queryData});
};

/**
 * 查询检查项目关键字
 */
$('#seachDicKey').keydown(function(event){
    if(event.keyCode==13){
        //grid.submitFilter();
        SdsFormDicMatch.search();
    }
});

$(function () {
    //查询二级表头树
    var ztree = new $ZTree("fieldTree", Sys.backendPath + "/sds/sdsindfieldconfig/tree/");
    ztree.bindOnClick(SdsField.onClickDicSub);
    ztree.init();
    SdsField.ztree = ztree;
    fuzzySearch('fieldTree','#seachKey',null,true);

    //病种项目
    var formItemColunms = SdsFormItemMatch.initColumn();
    var formItemTable = new BSTable(SdsFormItemMatch.id, Sys.backendPath + "/sds/sdsindfieldconfig/listFormItem", formItemColunms);
    formItemTable.setPaginationType("server");
    formItemTable.setPageSize(10);
    formItemTable.setSubHeight(-10);
    formItemTable.setShowExport(false);
    formItemTable.setShowRefresh(false);
    formItemTable.setShowToggle(false);
    formItemTable.setShowColumns(false);
    formItemTable.setOnClickRow(SdsFormItemMatch.onClickDic)
    formItemTable.setQueryParams({"compare":"","fieldId":0,"entityId":0});
    //formItemTable.setQueryParams({"fieldId":0});
    //formItemTable.setQueryParams({"entityId":0});
    SdsFormItemMatch.table = formItemTable.init();

    //病种值域
    var defaultColunms = SdsFormDicMatch.initColumn();
    var table = new BSTable(SdsFormDicMatch.id, Sys.backendPath + "/sds/sdsindfieldconfig/listFormDic", defaultColunms);
    table.setPaginationType("server");
    table.setPageSize(10);
    table.setSubHeight(-10);
    table.setShowExport(false);
    table.setShowRefresh(false);
    table.setShowToggle(false);
    table.setShowColumns(false);
    table.setQueryParams({"compare":"","fieldId":0,"formItemId":0});
    //table.setQueryParams({"fieldId":0});
    //table.setQueryParams({"formItemId":0})
    SdsFormDicMatch.table = table.init();
});
